<?php
class Mproduct extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="products";
    }
    
    public function listAll($amount, $start) {
        $query=$this->db->query("select distinct p.*, c.cate_title, s.status_name from products as p
                                inner join categories as c on c.cate_id = p.cate_id
                                inner join status as s on s.status_id = p.status_id
                                left outer join ability as a on a.pro_id = p.pro_id
                                left outer join providers as prov on prov.prov_id = a.prov_id
                                limit $start, $amount");
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function getById($id) {
        $this->db->where("pro_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function search($title, $price_from, $price_to, $quantity_from, $quantity_to, $cate, $status, $amount, $start) {
        $t=$p=$q=$c=$s="'1=1'";
        if($title!="") $t="pro_title like '%".$title."%'";
        if($price_from!="" and $price_to!="") $p="pro_price between $from and $to";
        if($quantity_from!="" and $quantity_to!="") $q="pro_quantity between $quantity_from and $quantity_to";
        if($cate!=0) $c="p.cate_id = $cate";
        if($status!=0) $s="p.status_id = $status";
        $str="select distinct p.*, c.cate_title, s.status_name from products as p
                inner join categories as c on c.cate_id = p.cate_id
                inner join status as s on s.status_id = p.status_id
                left outer join ability as a on a.pro_id = p.pro_id
                left outer join providers as prov on prov.prov_id = a.prov_id
                where $t and $p and $q and $c and $s
                limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($title, $price_from, $price_to, $quantity_from, $quantity_to, $cate, $status) {
        $t=$p=$q=$c=$s="'1=1'";
        if($title!="") $t="pro_title like '%".$title."%'";
        if($price_from!="" and $price_to!="") $p="pro_price between $from and $to";
        if($quantity_from!="" and $quantity_to!="") $q="pro_quantity between $quantity_from and $quantity_to";
        if($cate!=0) $c="p.cate_id = $cate";
        if($status!=0) $s="p.status_id = $status";
        $str="select distinct count(*) as 'count' from products as p
                inner join categories as c on c.cate_id = p.cate_id
                inner join status as s on s.status_id = p.status_id
                left outer join ability as a on a.pro_id = p.pro_id
                left outer join providers as prov on prov.prov_id = a.prov_id
                where $t and $p and $q and $c and $s";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("pro_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("pro_id", $id);
        $this->db->delete($this->_table);
    }
    
    public function checkTitle($title, $id="") {
        if($id!="")
            $this->db->where("pro_id != ", $id);
        $this->db->where("pro_title", $title);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return false;
        else
            return true;
    }
}
?>